package com.yao.service.impl;

import com.yao.config.WechatAccountConfig;
import com.yao.enums.ResultEnum;
import com.yao.exception.WeixinSellException;
import com.yao.service.WxOpenService;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * User: 姚宏武
 * Date: 2018/3/7
 * Time: 16:27
 * Email：401369870@qq.com
 * Description:
 */
@Service
public class WxOpenServiceImpl implements WxOpenService {

    @Autowired
    private WechatAccountConfig accountConfig;

    @Override
    public WxMpOAuth2AccessToken getOAuth2AccessToken(String code) throws WxErrorException {
        WxMpOAuth2AccessToken wxMpOAuth2AccessToken = new WxMpOAuth2AccessToken();
        CloseableHttpClient httpCilent = HttpClients.createDefault();
        RequestConfig requestConfig = RequestConfig.custom()
                .setConnectTimeout(5000)   //设置连接超时时间
                .setConnectionRequestTimeout(5000) // 设置请求超时时间
                .setSocketTimeout(5000)
                .setRedirectsEnabled(true)//默认允许自动重定向
                .build();

        String url = String.format(WxMpService.OAUTH2_ACCESS_TOKEN_URL, accountConfig.getOpenAppId(),
                accountConfig.getOpenAppSecret(), code);

        HttpGet httpGet = new HttpGet(url);
        httpGet.setConfig(requestConfig);
        String srtResult = "";
        try {
            HttpResponse httpResponse = httpCilent.execute(httpGet);
            if (httpResponse.getStatusLine().getStatusCode() == 200) {
                srtResult = EntityUtils.toString(httpResponse.getEntity());//获得返回的结果
                // 将返回的json串转为Java对象
                wxMpOAuth2AccessToken = WxMpOAuth2AccessToken.fromJson(srtResult);
            } else if (httpResponse.getStatusLine().getStatusCode() == 400) {
                throw new WeixinSellException(ResultEnum.WECHAT_OPEN_ERROR.getCode(), "微信开发平台相关错误");
            } else if (httpResponse.getStatusLine().getStatusCode() == 500) {
                throw new WeixinSellException(ResultEnum.WECHAT_OPEN_ERROR.getCode(), "微信开发平台相关错误");
            }
        } catch (IOException e) {
            throw new WeixinSellException(ResultEnum.WECHAT_OPEN_ERROR.getCode(), e.getMessage());
        } finally {
            try {
                httpCilent.close();
            } catch (IOException e) {
                throw new WeixinSellException(ResultEnum.WECHAT_OPEN_ERROR.getCode(), e.getMessage());
            }
        }
        return wxMpOAuth2AccessToken;
    }
}
